package com.vast.ui.iopanel.views;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.ViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import socketinterface.CommunicationManager;

import com.vast.ui.component.models.Io;
import com.vast.ui.component.models.IoPanel;
import com.vast.ui.component.providers.IoContentProvider;
import com.vast.ui.component.providers.IoEditingSupport;
import com.vast.ui.component.providers.IoLabelProvider;
import com.vast.ui.component.providers.IoModifier;


import com.vast.ui.iopanel.Activator;

public class ViewPart extends org.eclipse.ui.part.ViewPart {
	public Display    display;
	public Image      image;
	public IoPanel    ioPanel;
	public TableViewer viewer;
	public CommunicationManager communicationManager;
	
	public ViewPart() {
		// TODO Auto-generated constructor stub
	}
	public void initIoPanel(TableViewer viewer, final IoPanel ioPanel, int numberOfIo, int numberOfRaw) {
		// Create initial data of MemoryImage
		ioPanel.getList().clear();
		
		for(int i = 0; i < numberOfRaw; i++) {
			Io io = new Io(numberOfIo, numberOfRaw);	
			ioPanel.add(io);
		}

		viewer.setInput(ioPanel);
	}
	
	private void createColumns(final TableViewer viewer, int numberOfColumn) {
		Table table = viewer.getTable();

		// Define Column
		for (int i = 0; i < numberOfColumn; i++) {
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
			if(i%numberOfColumn ==0){
				column.getColumn().setWidth(0);
			}else {
				column.getColumn().setText(Integer.toString(i-1));
				column.getColumn().setResizable(false);
				column.getColumn().setMoveable(false);
				column.getColumn().setWidth(42);
				column.setEditingSupport(new IoEditingSupport(viewer, i-1, numberOfColumn -1, communicationManager));
			}
		}
			
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
	}
	
	public TableViewer createTableViewer(Composite parent, int numberOfColumn) {
		// Set ContentProvider
		TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);

		createColumns(viewer, numberOfColumn + 1);
		
		// Set ContentProvider
		viewer.setContentProvider(new IoContentProvider());
		
		// Set LabelProvider
		viewer.setLabelProvider(new IoLabelProvider(parent.getDisplay()));
		
		return viewer;
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		display    = parent.getDisplay();
		communicationManager = new CommunicationManager(this, "localhost", 30000);
		
		viewer = createTableViewer(parent, 8);
		ioPanel = new IoPanel();
		initIoPanel(viewer, ioPanel, 8, 4);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
